const fs = require('fs')
const path = require('path')
const { loadConfig } = require('./configuration')

const config = {
  cwd: '.',
  config: undefined,
  rootDir: ''
}

const CONFIG_JS_FILENAME = 'code-help.config.js'

let absoluteCwd = path.resolve(config.cwd)
let configFilePath = path.join(absoluteCwd, CONFIG_JS_FILENAME)

const getConfig = (name) => {
  const obj = {}
  if (fs.existsSync(configFilePath)) {
    const conf = loadConfig(configFilePath)

    if (conf && conf.options && conf.options[name]) {
      Object.assign(obj, config, conf.options[name])
    }
  }

  return obj
}

module.exports = {
  _absoluteCwd: absoluteCwd,
  getConfig
}
